# -*- coding: utf-8 -*-
#
#  Copyright (c) 2007, 2008 Guilherme Mesquita Gondim and contributors
#
#  This file is part of Django Diário.
#
#  Django Diário is free software under terms of the GNU Lesser
#  General Public License version 3 (LGPLv3) as published by the Free
#  Software Foundation. See the file README for copying conditions.
#

from django.conf import settings
from django.contrib.comments.models import Comment
from django.contrib.syndication.feeds import Feed
from django.utils.feedgenerator import Atom1Feed
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _

class RssCommentsFeed(Feed):
    description = _('Latest comments on Weblog')
    title_template = 'feeds/comments_title.html'
    description_template = 'feeds/comments_description.html'

    def title(self):
        if not hasattr(self, '_site'):
            self._site = Site.objects.get_current()
        return _("%(title)s's Weblog comments") % \
               {'title': self._site.name}

    def link(self):
        return reverse('diario-entry-list')

    def item_pubdate(self, item):
        return item.submit_date

    def get_query_set(self):
        get_list_function = Comment.objects.filter
        kwargs = {
            'is_public': True,
            'site__pk': settings.SITE_ID,
            'content_type__app_label__exact': 'diario',
            'content_type__model__exact': 'entry',
        }
        return get_list_function(**kwargs).order_by('-submit_date')

    def items(self):
        return self.get_query_set()[:30]

class AtomCommentsFeed(RssCommentsFeed):
    feed_type = Atom1Feed
    subtitle = RssCommentsFeed.description
